package petrochina.company.electric.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import petrochina.company.electric.R;
import petrochina.company.electric.app.AppAdapter;
import petrochina.company.electric.http.api.LoginApi;
import petrochina.company.electric.http.api.TaskListApi;
import petrochina.company.electric.model.TaskResult;

/**
 * desc   : 状态数据列表
 */
public final class TaskAdapter extends AppAdapter<TaskListApi.Bean> {
    private LoginApi.Bean mLoginData;

    public TaskAdapter(Context context, LoginApi.Bean loginData) {
        super(context);
        this.mLoginData = loginData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final CardView cvContent;
        private final TextView tvTaskNumberLine;
        private final TextView tvTaskNumber;
        private final TextView tvTaskDateTime;
        private final TextView tvTaskName;
        private final TextView tvTaskStepNum;
        private final TextView tvTaskStatus;
        private final TextView tv_task_worker;

        private ViewHolder() {
            super(R.layout.task_item);
            cvContent = findViewById(R.id.cv_content);
            tvTaskNumberLine = findViewById(R.id.tv_task_number_line);
            tvTaskNumber = findViewById(R.id.tv_task_number);
            tvTaskDateTime = findViewById(R.id.tv_task_date_time);
            tvTaskName = findViewById(R.id.tv_task_name);
            tvTaskStepNum = findViewById(R.id.tv_task_step_num);
            tvTaskStatus = findViewById(R.id.tv_task_status);
            tv_task_worker = findViewById(R.id.tv_task_worker);

        }

        @Override
        public void onBindView(int position) {
            TaskListApi.Bean taskResult = getItem(position);
            tvTaskNumberLine.setText(taskResult.getStaffName() + "线");
            tvTaskNumber.setText(taskResult.getStaffId() + "#");
            tvTaskDateTime.setText(taskResult.getCreateTime() + "");
            tvTaskName.setText(taskResult.getName());
            tvTaskStepNum.setText("步骤数: " + taskResult.getStepCount());
            switch (taskResult.getState()) {
                case 2:
                    tvTaskStatus.setText("已完成");
                    tvTaskStatus.setBackgroundResource(R.drawable.shape_task_status_complete);
                    cvContent.setBackgroundResource(R.drawable.bg_task_list_border_green);
                    break;
                case 1:
                    tvTaskStatus.setText("进行中");
                    tvTaskStatus.setBackgroundResource(R.drawable.shape_task_status_underway);
                    cvContent.setBackgroundResource(R.drawable.bg_task_list_border_yellow);
                    break;
                case 0:
                    tvTaskStatus.setText("未完成");
                    tvTaskStatus.setBackgroundResource(R.drawable.shape_task_status_unfinished);
                    cvContent.setBackgroundResource(R.drawable.bg_task_list_border_red);
                    break;
                default:
            }
            // 非监管身份 且任务非本人领取
            if (mLoginData.getDutyType() != 1 && taskResult.getStaffId() != 0 && mLoginData.getId() != taskResult.getStaffId()){
                tvTaskStatus.setBackgroundResource(R.drawable.shape_task_status_unreceive);
                cvContent.setBackgroundResource(R.drawable.bg_task_list_border_blue);
            }
            tv_task_worker.setVisibility(TextUtils.isEmpty(taskResult.getStaffName()) ? View.GONE : View.VISIBLE);
            tv_task_worker.setText(!TextUtils.isEmpty(taskResult.getStaffName()) ? "操作人：" + taskResult.getStaffName() : "");
        }
    }
}